package com.demo.Main.StageOne;
import java.io.*;
import java.net.*;
public class TzpStage1 {
    public static void main(String[] args) {
        //每10秒测试一遍服务器列表中的服务器是否能正常连接
        while (true)
        {
            start();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static void start(){
        String fileName = "localfile/server.txt";
        //读取服务器列表
        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))){
            String line;
            //逐行读取服务器列表
            while ((line = reader.readLine()) != null) {
                // 创建一个新线程来检测每个服务器
                Thread thread = new CheckServer(line);
                thread.start();
            }
        } catch (IOException e) {
            System.out.println("从文件读取服务器列表失败!" );
            e.printStackTrace();
        }
    }
}
class CheckServer extends Thread {
    private String serverUrl;
    public CheckServer(String serverUrl) {
        this.serverUrl = serverUrl;
    }
    @Override
    public void run() {
        try {
            //创建URL对象
            URL url = new URL(serverUrl);
            //打开HTTP连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            //发送请求并获取响应码
            int responseCode = connection.getResponseCode();
            //检查响应码是否为200（代表连接正常与否）
            if (responseCode == 200) {
                System.out.println(serverUrl + "连接成功");
            } else {
                System.out.println(serverUrl + "连接失败！" + responseCode);
            }
        } catch (IOException e) {
            System.out.println(serverUrl + "连接失败!");
        }
    }
}
